package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.DemandOfferBean;
import com.sgb.zhaocai.entity.MapLongAndLatEntity;
import com.sgb.zhaocai.entity.OfferDetailsEntitly;
import com.sgb.zhaocai.view.demand.activity.DemandOfferActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DemandOfferModel  extends ViewModel {

    private String longitude = "";
    private String latitude = "";

    /**
     * 我的报价-报价详情获取
     * @param activity
     * @param id
     * @param type
     */
    public void getOfferDetails(DemandOfferActivity activity, String id, String type) {
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (TextUtils.isEmpty(longitude)) {
            longitude = "0";
        }
        if (TextUtils.isEmpty(latitude)) {
            longitude = "0";
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("businessNo",id);
        map.put("type",type);
        map.put("localLng",longitude);//本地经度值
        map.put("localLat",latitude);//本地纬度值
        BaseApi.getApi().getFindNoticeOffer(map).enqueue(new ICallBack<BaseEntity<OfferDetailsEntitly>>() {
            @Override
            public void onSuccess(BaseEntity<OfferDetailsEntitly> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData()!=null) {
                        activity.setOfferDetails(data.getData());
                    } else {
                        MToast.showToast(activity, "获取订单信息失败");
                    }
                }
            }
        });
    }

    /**
     * 上传图片
     * @param file
     */
    public void uploadAvatarFileModel(DemandOfferActivity activity,File file,int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "上传中" + "...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(activity, "上传失败！");
                    return;
                }
                if (response.body().getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.uploadAvatarResult(response.body().getData().toString());
                } else {
                    MToast.showToast(activity, "上传失败！");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "上传失败！" + t.getMessage());
            }
        });
    }

    /**
     * 地址信息
     * @param activity
     */
    public void getAddressModel(DemandOfferActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setAddressList(data.getData());
                }
            }
        });
    }

    /**
     * 获取经纬度信息
     * @param activity
     * @param provinceNo
     * @param cityNo
     * @param areaNo
     */
    public void getLocationLL(DemandOfferActivity activity, String provinceNo, String cityNo, String areaNo) {
        String areaCode = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.AppProvinceCode, "");
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLocationLL(areaCode,provinceNo,cityNo,areaNo).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.updateAddress(data.getData());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 新增报价
     * @param activity
     * @param bean
     */
    public void getSubDataOffer(DemandOfferActivity activity, DemandOfferBean bean) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getsave(bean).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setSubDataOffer(data.getData().toString());
                }else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

}
